package com.basics.post.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.basics.post.entity.CyChannelPostFile;
import com.basics.post.mapper.CyChannelPostFileMapper;
import com.basics.post.service.CyChannelPostFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 帖子附件 服务实现类
 * </p>
 *
 * @author WangWei
 * @since 2025-04-17
 */
@Service
public class CyChannelPostFileServiceImpl extends ServiceImpl<CyChannelPostFileMapper, CyChannelPostFile> implements CyChannelPostFileService {

    @Override
    public List<CyChannelPostFile> selectPostFileList(Long postId, Long limit){
        return list(new QueryWrapper<CyChannelPostFile>()
                .eq("post_id",postId)
                .orderByAsc("order_by").last(limit!=null,"limit "+limit));
    }

    @Override
    public Map<Long,List<CyChannelPostFile>> selectPostFileList(List<String> postIds){
        List<CyChannelPostFile> list= list(new QueryWrapper<CyChannelPostFile>()
                .in("post_id",postIds)
                .orderByAsc("order_by"));
        Map<Long,List<CyChannelPostFile>> map = new HashMap<>();
        if(CollectionUtils.isNotEmpty(list)){
            for(CyChannelPostFile v:list) {
                if (map.containsKey(v.getPostId())) {
                    map.get(v.getPostId()).add(v);
                } else {
                    List<CyChannelPostFile> files = new ArrayList<>();
                    files.add(v);
                    map.put(v.getPostId(), files);
                }
            }
        }
        return map;
    }
}
